package xyz.yuzegod.megawalls.classes.novice.enderman;

import net.citizensnpcs.api.CitizensAPI;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.Plugin;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import xyz.yuzegod.megawalls.MegaWalls;
import xyz.yuzegod.megawalls.classes.*;
import xyz.yuzegod.megawalls.event.PlayerEnergyChangeEvent;
import xyz.yuzegod.megawalls.event.PlayerKillEvent;
import xyz.yuzegod.megawalls.game.GamePlayer;
import xyz.yuzegod.megawalls.util.ItemBuilder;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Enderman extends Classes
{
    public static final Map<GamePlayer, Integer> skillCooldown;
    
    public Enderman() {
        super("Enderman", "末影人", ChatColor.DARK_PURPLE, Material.ENDER_PEARL, (byte)0, ClassesType.NOVICE, new ClassesInfo.Orientation[] { ClassesInfo.Orientation.AGILITY, ClassesInfo.Orientation.CHARGER }, ClassesInfo.Difficulty.TWO);
        this.setMainSkill(new MainSkill(this));
        this.setSecondSkill(new SecondSkill(this));
        this.setThirdSkill(new ThirdSkill(this));
        this.setCollectSkill(new FourthSkill(this));
        this.setDefaultSkin("eyJ0aW1lc3RhbXAiOjE1NDkxNTU5OTY3MzEsInByb2ZpbGVJZCI6IjU2Njc1YjIyMzJmMDRlZTA4OTE3OWU5YzkyMDZjZmU4IiwicHJvZmlsZU5hbWUiOiJUaGVJbmRyYSIsInNpZ25hdHVyZVJlcXVpcmVkIjp0cnVlLCJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvODQ0ZGIyNzEwNmZhYTYwYzRhOWE0ZDNlMmE0MDVlOGM2ODYzNWJjYzZkNjViMDhjZDI5NzllNjA2MTNkNTMwMiJ9fX0=", "KTYuXTQQ63ew7EksUSwP5XEQtdUkZh7G8ZoX4mBh10fWX0OXJIURP0c7PxW7BiPfh3o5Fo5zSeUIEHcbW9tjvhxmGkjONviLkA13VehIGMIo5h5akdkhEajLJgxJyOJSlLxicFhOoEy4cVDZwmAgwQrqidyH8ipOw9Hm4RbL2mKRX3Q+oEwyc4eDetIGbLTsoeueUq88QvtfISK2f3vDftIN92mznZcP3/k/tT3Y/7CO2iGEmjpMADhrbgxCk7AgjdWhZoz43xTJED631j5JuQbnO7/eaiQXIqpBAWTWFhRX/m3b7W8znbmAemgG5LjfertqFQNseICMmvFzUjDUHbYZ82Nmvu+ii1KoIOhXGQG1mI7ri/oejdXCvg8kAfp0HrNE9GdzvPItqBDC7OCwNzLkOI7T75hXhyABppUdty52GJ2i0GHQl679Vhs5bRhnRjlyPCUvhFDrgLr4cZgXaWmaDiEJ5DjxAiJOV9UDvXOxovb1mQlhoTCytQVhXSeuKLsNlVpoWKgEPRIrDWlqUadpKtsL14+t0p9zg+69FCmpYF1nWLPxKCC3zmFU3ixr3PVIzrUJ5VOBlee7xBx7pVeIuodYKcKduQNLkHjoV8o/gtaj9Bc3ohjVgygcRazrtePmm/ZbOCtrUjyC+pFmDgnIZE0m2KkLkcEdawzB5Zo=");
        this.setEquipmentPackage(new EquipmentPackage(this) {
            @Override
            public List<ItemStack> getEquipments(final int level) {
                final List<ItemStack> items = new ArrayList<ItemStack>();
                items.add(new ItemBuilder(Material.COMPASS).setDisplayName(Enderman.this.nameColor + Enderman.this.getDisplayName() + " 指南针").build());
                items.add(new ItemBuilder(Material.DIAMOND_PICKAXE).setDisplayName(Enderman.this.nameColor + Enderman.this.getDisplayName() + " 镐").setUnbreakable(true).addEnchantment(Enchantment.DIG_SPEED, 3).addEnchantment(Enchantment.DURABILITY, 3).build());
                items.add(new ItemBuilder(Material.ENDER_CHEST).setDisplayName(Enderman.this.nameColor + Enderman.this.getDisplayName() + " 末影箱").build());
                switch (level) {
                    case 1: {
                        items.add(new ItemBuilder(Material.IRON_SWORD).setDisplayName(Enderman.this.nameColor + Enderman.this.getDisplayName() + " 剑").setUnbreakable(true).addEnchantment(Enchantment.DURABILITY, 10).build());
                        items.add(new ItemBuilder(Material.COOKED_BEEF).setDisplayName(Enderman.this.nameColor + Enderman.this.getDisplayName() + " 牛排").build());
                        items.add(new ItemBuilder(Material.IRON_BOOTS).setDisplayName(Enderman.this.nameColor + Enderman.this.getDisplayName() + " 靴子").setLore("§975%普通摔落伤害").setUnbreakable(true).addEnchantment(Enchantment.DURABILITY, 10).build());
                        break;
                    }
                    case 2: {
                        items.add(new ItemBuilder(Material.IRON_SWORD).setDisplayName(Enderman.this.nameColor + Enderman.this.getDisplayName() + " 剑").setUnbreakable(true).addEnchantment(Enchantment.DURABILITY, 10).build());
                        items.add(new ItemBuilder(Material.COOKED_BEEF, 2).setDisplayName(Enderman.this.nameColor + Enderman.this.getDisplayName() + " 牛排").build());
                        items.add(new ItemBuilder(Material.IRON_BOOTS).setDisplayName(Enderman.this.nameColor + Enderman.this.getDisplayName() + " 靴子").setLore("§975%普通摔落伤害").setUnbreakable(true).addEnchantment(Enchantment.DURABILITY, 10).build());
                        break;
                    }
                    case 3: {
                        items.add(new ItemBuilder(Material.IRON_SWORD).setDisplayName(Enderman.this.nameColor + Enderman.this.getDisplayName() + " 剑").setUnbreakable(true).addEnchantment(Enchantment.DURABILITY, 10).build());
                        items.add(new ItemBuilder(Material.COOKED_BEEF, 3).setDisplayName(Enderman.this.nameColor + Enderman.this.getDisplayName() + " 牛排").build());
                        items.add(new ItemBuilder(Material.IRON_BOOTS).setDisplayName(Enderman.this.nameColor + Enderman.this.getDisplayName() + " 靴子").setLore("§975%普通摔落伤害").setUnbreakable(true).addEnchantment(Enchantment.DURABILITY, 10).build());
                        items.add(new ItemBuilder(Material.POTION, 1, (byte)5).setDisplayName(Enderman.this.nameColor + "末影人 治疗药水 (8❤)").addPotion(new PotionEffect(PotionEffectType.HEAL, 1, 2)).build());
                        break;
                    }
                    case 4: {
                        items.add(new ItemBuilder(Material.IRON_SWORD).setDisplayName(Enderman.this.nameColor + Enderman.this.getDisplayName() + " 剑").setUnbreakable(true).addEnchantment(Enchantment.DURABILITY, 10).build());
                        items.add(new ItemBuilder(Material.COOKED_BEEF, 3).setDisplayName(Enderman.this.nameColor + Enderman.this.getDisplayName() + " 牛排").build());
                        items.add(new ItemBuilder(Material.IRON_BOOTS).setDisplayName(Enderman.this.nameColor + Enderman.this.getDisplayName() + " 靴子").setLore("§975%普通摔落伤害").setUnbreakable(true).addEnchantment(Enchantment.DURABILITY, 10).build());
                        items.add(new ItemBuilder(Material.POTION, 1, (byte)5).setDisplayName(Enderman.this.nameColor + Enderman.this.getDisplayName() + " 治疗药水 (8❤)").addPotion(new PotionEffect(PotionEffectType.HEAL, 1, 2)).build());
                        items.add(new ItemBuilder(Material.POTION, 1, (byte)2).setDisplayName(Enderman.this.nameColor + Enderman.this.getDisplayName() + " 速度药水 II (0:15秒)").addPotion(new PotionEffect(PotionEffectType.SPEED, 300, 1)).build());
                        break;
                    }
                    case 5: {
                        items.add(new ItemBuilder(Material.IRON_SWORD).setDisplayName(Enderman.this.nameColor + Enderman.this.getDisplayName() + " 剑").setUnbreakable(true).addEnchantment(Enchantment.DURABILITY, 10).build());
                        items.add(new ItemBuilder(Material.COOKED_BEEF, 3).setDisplayName(Enderman.this.nameColor + Enderman.this.getDisplayName() + " 牛排").build());
                        items.add(new ItemBuilder(Material.DIAMOND_BOOTS).setDisplayName(Enderman.this.nameColor + Enderman.this.getDisplayName() + " 靴子").setLore("§975%普通摔落伤害").setUnbreakable(true).addEnchantment(Enchantment.DURABILITY, 10).build());
                        items.add(new ItemBuilder(Material.POTION, 2, (byte)5).setDisplayName(Enderman.this.nameColor + Enderman.this.getDisplayName() + " 治疗药水 (8❤)").addPotion(new PotionEffect(PotionEffectType.HEAL, 1, 2)).build());
                        items.add(new ItemBuilder(Material.POTION, 2, (byte)2).setDisplayName(Enderman.this.nameColor + Enderman.this.getDisplayName() + " 速度药水 II (0:15秒)").addPotion(new PotionEffect(PotionEffectType.SPEED, 300, 1)).build());
                        break;
    }
                }
                if (xyz.yuzegod.megawalls.util.ItemUtils.containsBow(items)) {
                    return items;
                }else {
                    items.add(new ItemBuilder(Material.BOW).setDisplayName(nameColor+getDisplayName()+" 弓").setUnbreakable(true).addEnchantment(Enchantment.DURABILITY,10).build());
                    return items;
                }
            }
            
            @Override
            public List<String> getInfo(final int level) {
                final List<String> lore = new ArrayList<String>();
                switch (level) {
                    case 1: {
                        lore.add(" §8▪ §7铁 剑");
                        lore.add("    §8▪ 耐久 X");
                        lore.add(" §8▪ §7铁 靴子");
                        lore.add("    §8▪ 耐久 X");
                        lore.add(" §8▪ §7牛排");
                        break;
                    }
                    case 2: {
                        lore.add(" §8▪ §7牛排 §8x1 ➜ §ax2");
                        break;
                    }
                    case 3: {
                        lore.add(" §a+ §7治疗药水 8❤");
                        lore.add(" §8▪ §7牛排 §8x2 ➜ §ax3");
                        break;
                    }
                    case 4: {
                        lore.add(" §a+ §7速度药水 II");
                        lore.add("    §8▪ 0:15");
                        break;
                    }
                    case 5: {
                        lore.add(" §8▪ §7铁 §8➜ §a钻石 §7靴子");
                        lore.add("    §8▪ 耐久 X");
                        lore.add(" §8▪ §7治疗药水 8❤ §8x1 ➜ §ax2");
                        lore.add(" §8▪ §7速度药水 II §8x1 ➜ §ax2");
                        lore.add("    §8▪ 0:15");
                        break;
                    }
                }
                return lore;
            }
        });
    }
    
    @Override
    public List<String> getInfo() {
        return new ArrayList<String>();
    }
    
    @Override
    public int unlockCost() {
        return 0;
    }
    
    @Override
    public int energyMelee() {
        return 30;
    }
    
    @Override
    public int energyBow() {
        return 30;
    }
    
    @Override
    public String getSkillTip(final GamePlayer gamePlayer) {
        return this.mainSkill.getSkillTip(gamePlayer);
    }
    
    @Override
    public void run() {
        GamePlayer.getOnlinePlayers().stream().filter(gamePlayer -> !gamePlayer.isSpectator() && ClassesManager.getSelected(gamePlayer).equals(this)).forEach(gamePlayer -> {
            if (Enderman.skillCooldown.getOrDefault(gamePlayer, 0) > 0) {
                Enderman.skillCooldown.put(gamePlayer, Enderman.skillCooldown.get(gamePlayer) - 1);
            }
            gamePlayer.sendActionBar(this.getSkillTip(gamePlayer));
        });
    }
    
    @EventHandler
    public void onPlayerEnergyChange(final PlayerEnergyChangeEvent e) {
        final GamePlayer player = e.getPlayer();
        final Classes classes = ClassesManager.getSelected(player);
        if (classes.equals(this)) {
            if (player.getPlayer().hasPotionEffect(PotionEffectType.SPEED)) {
                e.setAmount(e.getAmount() / 2);
            }
            if (player.getEnergy() + e.getAmount() >= 100) {
                this.thirdSkill.use(player, player.getPlayerStats().getKitStats(classes));
            }
        }
    }
    
    @EventHandler
    public void onPlayerKill(final PlayerKillEvent e) {
        final GamePlayer killer = e.getKiller();
        final Classes classes = ClassesManager.getSelected(killer);
        if (classes.equals(this) && killer.getGameTeam().isWitherDead()) {
            this.secondSkill.use(killer, killer.getPlayerStats().getKitStats(classes));
        }
    }
    
    @EventHandler(priority = EventPriority.LOWEST)
    public void onPlayerDeath(final PlayerDeathEvent e) {
        if (MegaWalls.getInstance().getGame().isWaiting()) {
            return;
        }
        final GamePlayer player = GamePlayer.get(e.getEntity().getUniqueId());
        final Classes classes = ClassesManager.getSelected(player);
        if (classes.equals(this) && !player.getGameTeam().isWitherDead() && MegaWalls.getRandom().nextInt(300) <= this.secondSkill.getAttribute(player.getPlayerStats().getKitStats(classes).getSkill2Level()) * 100.0) {
            player.sendMessage("你的末影之心技能保存在你的物品栏中！");
            Bukkit.getScheduler().runTaskLater((Plugin)MegaWalls.getInstance(), (Runnable)new Runnable() {
                @Override
                public void run() {
                    for (final ItemStack itemStack : e.getDrops()) {
                        if (itemStack.getType() != Material.POTION && itemStack.getType() != Material.DIAMOND && itemStack.getType() != Material.IRON_INGOT) {
                            if (ClassesManager.isClassesItem(itemStack)) {
                                continue;
                            }
                            player.getPlayer().getInventory().addItem(new ItemStack[] { itemStack });
                        }
                    }
                    player.sendMessage("你的末影之心技能保存在你的物品栏中！");
                }
            }, 5L);
        }
    }
    
    @EventHandler(priority = EventPriority.LOWEST)
    public void onEntityDamage(final EntityDamageEvent e) {
        if (CitizensAPI.getNPCRegistry().isNPC(e.getEntity())) {
            return;
        }
        if (e.isCancelled()) {
            return;
        }
        if (e.getEntity() instanceof Player && e.getCause() == EntityDamageEvent.DamageCause.FALL) {
            final GamePlayer player = GamePlayer.get(e.getEntity().getUniqueId());
            final Classes classes = ClassesManager.getSelected(player);
            if (classes.equals(this)) {
                e.setDamage(EntityDamageEvent.DamageModifier.ARMOR, -e.getDamage() * 0.75);
            }
        }
    }
    
    @EventHandler(priority = EventPriority.LOWEST)
    public void onEntityDamageByEntity(final EntityDamageByEntityEvent e) {
        if (CitizensAPI.getNPCRegistry().isNPC(e.getEntity())) {
            return;
        }
        if (e.isCancelled()) {
            return;
        }
        if (e.getEntity() instanceof Player && e.getDamager() instanceof Player) {
            final GamePlayer damager = GamePlayer.get(e.getDamager().getUniqueId());
            if (damager.isSpectator() || damager.getGameTeam().isInTeam(GamePlayer.get(e.getEntity().getUniqueId()))) {
                return;
            }
            final Classes classes = ClassesManager.getSelected(damager);
            if (classes.equals(this)) {
                this.thirdSkill.use(damager, damager.getPlayerStats().getKitStats(classes));
            }
        }
    }
    
    static {
        skillCooldown = new HashMap<GamePlayer, Integer>();
    }
}
